
package com.cpp.server.aspect;

import com.cpp.pojo.pojo.OperationLog;
import com.cpp.pojo.pojo.User;
import com.cpp.server.aspect.annotation.IgnoreLog;
import com.cpp.server.aspect.annotation.MyLog;
import com.cpp.server.context.UserHolder;
import com.cpp.server.mapper.OperationLogMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
public class LogAspect {
    private final OperationLogMapper operationLogMapper;

//    @Pointcut("@annotation(com.cpp.server.aspect.annotation.IgnoreLog)")
//    public void ignoreLog() {
//    }

    //方法上有MyLog
    @Around(value = "@annotation(myLog)") // 现在能正确识别 Log 注解
    public Object log(ProceedingJoinPoint joinPoint, MyLog myLog) throws Throwable {
        Object result = joinPoint.proceed();
        String module = myLog.module();

        User currentUser = UserHolder.getCurrentUser();
        Long userId = currentUser.getId();   // 从登录上下文获取当前用户ID
        String role = currentUser.getRole(); // 获取角色ID（1: 教师，2: 学生）

        long startTime = System.currentTimeMillis();
        Date startDateTime = new Date();


        long durationMs = System.currentTimeMillis() - startTime;
        //转成秒
        Double durationMin = durationMs / 1000.0;
        // 构建日志对象并保存
        saveOperationLog(userId, role, module,joinPoint, startDateTime, new Date(), durationMin);

        return result;
    }

    @Pointcut("!@annotation(com.cpp.server.aspect.annotation.MyLog)&&!@annotation(com.cpp.server.aspect.annotation.IgnoreLog)")
    public void normalLog() {
    }

    //方法上无MyLog，类上有
    @Around("normalLog()&&@within(myLog)")
    public Object log2(ProceedingJoinPoint joinPoint,MyLog myLog) throws Throwable {
        Object result = joinPoint.proceed();
        String module = myLog.module();

        User currentUser = UserHolder.getCurrentUser();
        Long userId = currentUser.getId();   // 从登录上下文获取当前用户ID
        String role = currentUser.getRole(); // 获取角色ID（1: 教师，2: 学生）

        long startTime = System.currentTimeMillis();
        Date startDateTime = new Date();


        long durationMs = System.currentTimeMillis() - startTime;
        //转成秒
        Double durationMin = durationMs / 1000.0;
        // 构建日志对象并保存
        saveOperationLog(userId, role, module,joinPoint, startDateTime, new Date(), durationMin);

        return result;
    }

    private void saveOperationLog(Long userId, String role, String module,
                                  ProceedingJoinPoint joinPoint, Date startTime, Date endTime, Double durationMin) {
        log.info("用户ID: {}, 角色: {}, 模块: {},方法{}， 开始时间: {}, 结束时间: {}, 耗时: {} 秒",
                userId, role, module, joinPoint.getSignature().getName(),startTime, endTime, durationMin);

        // 构建日志对象并保存
        OperationLog operationLog = OperationLog.builder()
                .userId(userId)
                .role(role)
                .module(module)
                .startTime(startTime)
                .endTime(endTime)
                .durationMin(durationMin)
                .build();
        operationLogMapper.insert(operationLog);
    }


}